import common from '../vendor/common'
import '../scss/house_detail.scss'
import { houseDetail, makeOrder, collects, delcollects, auditToServer, getCollectStatus } from '../vendor/api'
import houseDetailList from '../components/houseDetailList'
import selfMap from '../components/detailMap'
try{
const mui = common.mui
const Vue = common.Vue
const detailBanner = common.detailBanner
var Vuehouse = null //详情页数据Vue对象
var ids = '' // 接受的id
Vuehouse = new Vue({
  el: '#Vuehouse',
  components: { houseDetailList, detailBanner, selfMap },
  data: {
    list: {
      id: '', // 房源id
      selfName: '', // 客户姓名
      selfTel: '', // 客户电话
      shortest_lease_cn: '', // 最短租期
      building_name: '', // 所属楼盘名称
      unit_price: '', //租金
      total_price_cn: '', //总价格
      constru_acreage: '', //面积
      station_number: '', //工位数
      payment_type_cn: '', //支付方式
      renovation_cn: '', //装修
      register_company_cn: '', //注册公司
      check_in_time_cn: '', //入住时间
      shortest_lease: '', //最短租期
      floor_cn: '', //楼层
      house_type: '', // 户型
      office_building_type_cn: '', //类型
      orientation_cn: '', //朝向
      open_bill_cn: '', //可否开发票
      split_cn: '', //可否拆分
      address: '', //地址
      class: '', // 等级
      structure: '', // 房屋结构
      total_floor: '', // 总楼层
      property_company: '', // 物业公司
      property_fee: '', // 物业费
      passenger_lift: '', // 客梯数量
      cargo_lift: '', // 货梯数量
      president_lift: '', // 总裁电梯
      title: '', // 标题
      air_conditioner: '', // 空调
      gps: ['114.31', '30.52'], // gps定位
      heating: '', // 采暖
      house_feature: '', // 房源优势
      collection: '', // 是否收藏
      label_cn: '', // 是否优选
      pic_url: [] // 轮播图
    },
    other: []
  },
  methods: {
    // 向客服打电话
    telToServer: function() {
      auditToServer('APP端房源详情').then(res => {
        console.log('记录成功')
      })
    },
    // 显示模态框
    showBox: function() {
      mui('.mui-popover').popover('show')
    },
    // 隐藏模态框
    hideBox: function() {
      mui('.mui-popover').popover('hide')
    },
    // 提交预约
    uploadBook: function() {
      mui('#tel')[0].blur()
      mui('#names')[0].blur()
      var name = this.list.selfName,
          tel = this.list.selfTel,
          telReg=/^[1][0-9]{10}$/;
      if (!name || name.trim() === '') {
        plus.nativeUI.toast('请输入称谓',{ duration:'long', type:'div' }   )
        return false
      }
      if (!telReg.test(tel)) {
        plus.nativeUI.toast('请输入11位手机号',{ duration:'long', type:'div' })
        return false
      }
      var params = { tel: tel, name: name, page_source: '房源详情-预约看房', source: 5, demand: 2 }
      makeOrder(params).then(res => {
        if (res.success) {
          mui('.mui-popover').popover('hide')
          plus.nativeUI.toast('信息提交成功，楚楼网30分钟内联系您',{ duration:'long', type:'div' })
        }
      })
    },
    // 收藏
    collects: function() {
      var token = plus.storage.getItem('access_token')
      if (token) {
        if (!this.list.collection) {
          var house_id = { house_id: this.list.id }
          collects(house_id).then(res => {
            if (res.success) {
              plus.nativeUI.toast('收藏成功',{ duration:'long', type:'div' })
              this.list.collection = true     
            }
          })
        } else {
          delcollects(this.list.id).then(res => {
            if (res.success) {
              plus.nativeUI.toast('取消收藏成功',{ duration:'long', type:'div' }) 
              this.list.collection = false    
            }
          })
        }
      }else {
        plus.webview.getWebviewById('login_new.html').show('slide-in-bottom', 300)
      }
    }
  }
})
mui.init({
    swipeBack:true, //启用右滑关闭功能
    pullRefresh : {
      container:"#pullrefresh",
      down: {
        style:'circle',//必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
        contentrefresh : "正在刷新...",
        color:'#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
        height:50,//可选,默认50px.下拉刷新控件的高度
        range:'100px', //可选 默认100px,控件可下拉拖拽的范围
        offset:'0px', //可选 默认0px,下拉刷新控件的起始位置
        callback: function() {
          init_index()
          mui('#pullrefresh').pullRefresh().endPulldownToRefresh()
        }
      }
    }
})

} catch(e){
    console.log('house_detail出错了')
}
var data = plus.webview.currentWebview().item
ids = data.id
for (var p in data) {
  Vuehouse.list[p] = data[p]
}
// 查询是否收藏
getCollectStatus(ids).then(res => {
  Vuehouse.list.collection = res.data
})
Vuehouse.$refs.houseDetailList.init(ids)
houseDetail(ids)
// 页面预加载数据
document.addEventListener('loadData', function(event) {
  window.scrollTo(0,0)
  ids = event.detail.item.id
  plus.webview.currentWebview().setStyle({titleNView:{titleText: event.detail.item.title}})
  for (var p in Vuehouse.list) {
    Vuehouse.list[p] = event.detail.item[p]
  }
  // 查询是否收藏
  getCollectStatus(ids).then(res => {
    Vuehouse.list.collection = res.data
  })
  Vuehouse.$refs.houseDetailList.init(ids)
  houseDetail(ids)
})
function init_index(){
  window.scrollTo(0,0)
  Vuehouse.$refs.houseDetailList.init(ids)
  houseDetail(ids).then(res => {
    for (var p in Vuehouse.list) {
      Vuehouse.list[p] = res.data[p]
    }
  }) 
}